package com.bdqn.zhang.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.LayData;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param model
     * @return
     */
    /**@RequestMapping(value = "/login")
    public String login(Model model) {
        return "user/login";
    }

    @RequestMapping(value = "/dologin")
    @ResponseBody
    public String dologin(HttpSession session, @RequestParam String username,@RequestParam String password) {
        User user1=userService.Login(username,password);
        if (user1 != null || !user1.equals("")) {
            return JSON.toJSONString("true");
            //return "user/login";
        }else {
            return JSON.toJSONString("false");
        }
    }*/
    @RequestMapping(value = "/login")
    public String login(Model model) {
        return "user/login";
    }

    @RequestMapping(value = "/dologin")
    public String dologin(User user, Model model, HttpSession session) {
        User user1=userService.dologin(user);
        if (user1 != null) {
            session.setAttribute("users",user);
            return "user/user";
        }else {
            session.setAttribute("error","用户名或密码不正确");
            return "user/login";
        }
    }
    @RequestMapping(value = "main")
    public String index(){
        return "index";
    }
    /**
     * 添加用户信息
     * @param user
     * @param
     * @return
     */
    @RequestMapping(value = "/addUser")
    @ResponseBody
    public String addUser(User user,HttpSession session){
        String createbys=session.getAttribute("username").toString();
        user.setCreateBy(createbys);
        int result=userService.addUser(user);
        return JSON.toJSONString(result);
    }

    /**
     * 修改用户信息
     * @param user
     * @param
     * @return
     */
    @RequestMapping(value = "/updateInfoById")
    @ResponseBody
    public String updateInfoById(User user){
        int result=userService.updateUser(user);
        return JSON.toJSONString(result);
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @RequestMapping(value = "/selectinfo")
    public String select(){
        List<User> user1=userService.selectAll(0,5);
        int count=userService.count();
        LayData lay=LayData.data(count,user1);
        if(lay!=null || !lay.equals("")){
            return JSON.toJSONString(lay);
        }else{
            return null;
        }
    }

    /**
     * 查询用户信息
     * @return
     */
    @RequestMapping(value = "/selectUserInfoById")
    @ResponseBody
    public String selectByid(@RequestParam("id")String id){
        User result=userService.selectById(Integer.valueOf(id));
        return JSON.toJSONString(result.toString());
    }

    /**
     * 删除用户
     */
    @RequestMapping(value = "/deleteUserInfoById")
    @ResponseBody
    public String delete(@RequestParam("id")String id){
        Integer result=userService.delUser(Integer.valueOf(id));
        return JSON.toJSONString(result.toString());
    }
}
